package com.gofunbuy.app.favourable;

import java.util.List;

import com.gofunbuy.R;
import com.gofunbuy.core.CouponEntity;
import com.gofunbuy.core.ProductEntity;
import com.gofunbuy.util.AsyncImageLoader;
import com.gofunbuy.util.AsyncImageLoader2.ImageCallback;
import com.gofunbuy.util.GoFunBuyUtils;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class AppFavourableAdapter extends BaseAdapter{

        private LayoutInflater mInflater;
        private List<CouponEntity> mData ;
        private ListView mListView;
        
       // private AsyncImageLoader asyncImageLoader;
        
        public AppFavourableAdapter(Context context,ListView listView){
            this.mInflater = LayoutInflater.from(context);
            this.mListView = listView;
           // asyncImageLoader = new AsyncImageLoader();
        }
        
        public List<CouponEntity> getData() {
			return mData;
		}

		public void setData(List<CouponEntity> mData) {
			this.mData = mData;
		}

		@Override
        public int getCount() {
            // TODO Auto-generated method stub
        	if(mData==null){
        		return 0;
        	}
            return mData.size();
        }
 
        @Override
        public CouponEntity getItem(int arg0) {
            // TODO Auto-generated method stub
            return mData.get(arg0);
        }
 
        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }
 
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
             
            ViewHolder holder = null;
            if (convertView == null) {
                 
                holder=new ViewHolder(); 
                 
                convertView = mInflater.inflate(R.layout.favourable_item_layout, null);
                holder.img = (ImageView)convertView.findViewById(R.id.favourable_item_img);
                holder.name = (TextView)convertView.findViewById(R.id.favourable_item_name);
                holder.des = (TextView)convertView.findViewById(R.id.favourable_item_des);
                holder.more = (TextView)convertView.findViewById(R.id.favourable_item_more);
                
                convertView.setTag(holder);
                 
            }else {
                 
                holder = (ViewHolder)convertView.getTag();
            }
            CouponEntity pe =  mData.get(position);    
            
            holder.name.setText(pe.getName());
            holder.des.setText(pe.getDep());
             
            if (pe.getImgDrawable() != null) {            	
            	holder.img.setImageDrawable(pe.getImgDrawable());
            	
    		} else if (pe.getImgPath() != null && !"".equals(pe.getImgPath())) {
    			String imageUrl = pe.getImgPath();
    			holder.img.setTag(imageUrl);

    			Drawable cachedImage = GoFunBuyUtils.asyncImageLoader.loadDrawable(imageUrl,
    					new ImageCallback() {
    						public void imageLoaded(Drawable imageDrawable,
    								String imageUrl) {
    							ImageView imageViewByTag = (ImageView) mListView
    									.findViewWithTag(imageUrl);
    							if (imageViewByTag != null) {
    								imageViewByTag.setImageDrawable(imageDrawable);
    							}

    						}
    					});
    			if (cachedImage == null) {
    				holder.img.setImageResource(R.drawable.loading);
    			} else {
    				holder.img.setImageDrawable(cachedImage);
    			}
    		}            
            holder.more.setOnClickListener(new View.OnClickListener() {
                
                @Override
                public void onClick(View v) {
               
                }
            });
             
            return convertView;
        }  
        public final class ViewHolder{
            public ImageView img;
            public TextView name;
            public TextView des;
            public TextView more;
        }  	
}